<cfcomponent>
	<!---
	CF-STORM - Super Tiny ORM
	Beau Holton
	--->
	
	<cfset variables.modeldirectory = ""/>
	<cfset variables.stormdirectory = ""/>
	<cfset variables.dsn = ""/>
	
	<cffunction name="init" access="public">
	
	</cffunction>
	
	<cffunction name="create" access="public">
		<cfargument name="objectname" required="true"/>
		<cfset var newobject = 0/>
		<cfset var NoFileBuilt = false/>
		<cfset var fileSeparator = createObject("java", "java.lang.System").getProperty("file.separator") />
		<cfset var filePath = Expandpath(fileSeparator & replace(variables.modeldirectory,'.',fileSeparator,'all')) & fileSeparator & arguments.objectname & ".cfc"/>
		<cfif ListContainsNoCase(StructkeyList(variables.objects),arguments.objectname)>
			<cfif NOT FileExists(filePath)>
				<cftry>
					<cfset buildObjectFile(arguments.objectname,filePath)/>
				<cfcatch type="any">
					<cfthrow message="Could not build object file #arguments.objectname#.cfc" type="cfstorm.buildobjectfailure"/>
				</cfcatch>
				</cftry>
			</cfif>
			<cftry>
				<cfset newobject = getObject(arguments.objectname)/>
			<cfcatch type="application">
				<cfdump var="#cfcatch#"/><cfabort>
				<cfthrow message="Could not create object #arguments.objectname#" type="storm.createobjectfailure"/>
			</cfcatch>
			</cftry>
		<cfelse>
			<cfthrow message="#arguments.objectname# is not an known object" type="storm.unknownobject"/>
		</cfif>
		<cfif NOT StructKeyExists(variables.objects["#objectname#"],"pkdefault")>
			<cfset variables.objects["#objectname#"].pkdefault = 0/>
		</cfif>
		<cfif NOT StructKeyExists(variables.objects["#objectname#"],"seq")>
			<cfset variables.objects["#objectname#"].seq = ""/>
		</cfif>
		<cfreturn newobject.init(variables.dsn,arguments.objectname,variables.objects["#objectname#"].attributes,variables.objects["#objectname#"].pk,variables.objects["#objectname#"].pkdefault,variables.objects["#objectname#"].seq) />
	</cffunction>
	
	<cffunction name="buildObjectFile" access="private">
		<cfargument name="objectname" required="true"/>
		<cfargument name="filePath" required="true"/>
		<cfset var fileSeparator = createObject("java", "java.lang.System").getProperty("file.separator") />
		<cfset var fileContents = "" />
		<cfoutput>
			<cfsavecontent variable="fileContents">
&lt;cfcomponent extends="#variables.stormdirectory#.stormbase" hint="I am an cf-storm object based on the #arguments.objectname# from #variables.dsn#"&gt;
	&lt;!---
	CF-STORM - Super Tiny ORM
	Beau Holton
	---&gt;

	&lt;!--- Place custom object functions below (e.g. validation functions, debugging functions)---&gt;
	
	&lt;cffunction name="save" access="public"&gt;
		&lt;cfset super.save()/&gt;
	&lt;/cffunction&gt;
	
	&lt;cffunction name="validate" access="public"&gt;
		&lt;cfset super.validate()/&gt;
	&lt;/cffunction&gt;
	
	
	
&lt;/cfcomponent&gt;
			</cfsavecontent>
		</cfoutput>
		<cfset fileContents = replace(fileContents,"&lt;","<","all")/>
		<cfset fileContents = replace(fileContents,"&gt;",">","all")/>
		<cffile action="write" file="#arguments.filePath#" output="#fileContents#" />
	</cffunction>
	
	<cffunction name="getObject" access="private">
		<cfargument name="objectname" required="true"/>
		<cfreturn createobject("component","#variables.modeldirectory#.#arguments.objectname#")/>
	</cffunction>
	
	<cffunction name="buildAttributes" access="private">
		<cfargument name="attributeList" required="true"/>
		<cfset var attributes = StructNew()/>
		<cfloop list="#arguments.attributeList#" index="attribute" delimiters=",">
			<cfset attributes[#ListGetAt(attribute,1,"|")#] = Structnew()/>
			<cfset attributes[#ListGetAt(attribute,1,"|")#].type = ListGetAt(attribute,2,"|")/>
			<cfset attributes[#ListGetAt(attribute,1,"|")#].length = ListGetAt(attribute,3,"|")/>
			<cfset attributes[#ListGetAt(attribute,1,"|")#].required = ListGetAt(attribute,4,"|")/>
			<cfset attributes[#ListGetAt(attribute,1,"|")#].hasdefault = ListGetAt(attribute,5,"|")/>
		</cfloop>
		<cfreturn attributes/>
	</cffunction>
	
	<cffunction name="createArray" access="public">
		<cfargument name="objectname" required="true"/> 
		<cfargument name="query" required="true"/>
		<cfset var objectarray = arraynew(1)/>
		<cfset var object = 0/>
		<cfset var arraycounter = 1/>
		
		<cftry>
			<cfif isQuery(arguments.query)>
				<cfloop query="arguments.query">
					<cfset object = this.create(arguments.objectname)/>
					<cfset object.loadFromQuery(arguments.query,arraycounter)/>
					<cfset arrayAppend(objectarray,object)/>
					<cfset arraycounter = arraycounter + 1/>
				</cfloop>
			</cfif> 
		<cfcatch type="any">
			<cfdump var="#cfcatch#"/><cfabort>
		</cfcatch>
		</cftry>

		<cfreturn objectarray/>
	</cffunction>
	
	
	
</cfcomponent>